//
//  VerifyVC.swift
//  Employee
//
//  Created by swordjoy on 2017/5/2.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class VerifyVC: BaseVC {
  
  fileprivate var contentV: UIView!
  
  fileprivate var phoneTextField: UITextField!
  
  fileprivate var verifyTextField: UITextField!
  
  fileprivate var nextButton: UIButton!
  
  fileprivate var verifyButton: VerifyButton!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    view.backgroundColor = UIColor.white
    title = "找回密码"
    
    create()
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    GlobalConfig.baiduStatisticsPageViewStart("找回密码")
  }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        GlobalConfig.baiduStatisticsPageViewEnd("找回密码")
    }

}

// MARK: - nextAction

extension VerifyVC {
  
  @objc fileprivate func verifyAction() {
    if phoneTextField.text!.sj.length != 11 {
      SJHint.show("请输入正确手机号码")
      return 
    }
    
    verifyButton.isCount = true
    
    let _ = UserRequest.shared.getModifyPasswordCode(phone: phoneTextField.text!)
  }
  
  @objc fileprivate func nextAction() {
    SJApp.endEdit()
    
    if !SJApp.isShowHUD { SJApp.showHUD() }
    let _ = UserRequest.shared.verifyCode(phone: phoneTextField.text!, code: verifyTextField.text!) {
      let vc = ModifyPasswordVC()
      vc.phone = self.phoneTextField.text!
      self.navigationController?.pushViewController(vc, animated: true)
    }
  }
  
  @objc fileprivate func phoneChangeAction() {
    nextButtonHelper()
  }
  
  @objc fileprivate func verifyChangeAction() {
    nextButtonHelper()
  }
  
}

// MARK: - fileprivate

extension VerifyVC {
  
  fileprivate func nextButtonHelper() {
    
    if phoneTextField.text!.sj.length == 11 && verifyTextField.text!.sj.length == 6 {
      nextButton.backgroundColor = GlobalConfig.BGSkyBlueColor
      nextButton.isEnabled = true
    }
    else {
      nextButton.backgroundColor = GlobalConfig.TextLightColor
      nextButton.isEnabled = false
    }
    
  }
  
  fileprivate func create() {
    
    addBackItem()
    
    contentV = UIView(frame: UIScreen.main.bounds)
    contentV.backgroundColor = GlobalConfig.BGWhiteColor
    view.addSubview(contentV)
    
    phoneTextField = UITextField()
    contentV.addSubview(phoneTextField)
    phoneTextField.addTarget(self, action: #selector(phoneChangeAction), for: .editingChanged)
    phoneTextField.placeholder = "手机号"
    phoneTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    phoneTextField.leftViewMode = .always
    phoneTextField.clearButtonMode = .whileEditing
    phoneTextField.rightViewMode = .always
    
    verifyButton = VerifyButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
    verifyButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    verifyButton.setTitle("获取验证码", for: .normal)
    verifyButton.titleLabel?.font = GlobalConfig.system15Font
    verifyButton.addTarget(self, action: #selector(verifyAction), for: .touchUpInside)
    phoneTextField.rightView = verifyButton
    
    phoneTextField.backgroundColor = UIColor.white
    phoneTextField.keyboardType = .numberPad
    phoneTextField.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(74)
    }
    phoneTextField.layer.cornerRadius = 4
    phoneTextField.clipsToBounds = true
    
    verifyTextField = UITextField()
    contentV.addSubview(verifyTextField)
    verifyTextField.addTarget(self, action: #selector(verifyChangeAction), for: .editingChanged)
    verifyTextField.placeholder = "验证码"
    verifyTextField.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 50))
    verifyTextField.leftViewMode = .always
    verifyTextField.clearButtonMode = .whileEditing
    verifyTextField.backgroundColor = UIColor.white
    verifyTextField.keyboardType = .numberPad
    verifyTextField.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(phoneTextField.snp.bottom).offset(10)
    }
    verifyTextField.layer.cornerRadius = 4
    verifyTextField.clipsToBounds = true
    
    nextButton = UIButton()
    contentV.addSubview(nextButton)
    nextButton.setTitle("下一步", for: .normal)
    nextButton.setTitleColor(UIColor.white, for: .normal)
    nextButton.titleLabel?.font = GlobalConfig.system15Font
    nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
    nextButton.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.height.equalTo(50)
      make.top.equalTo(verifyTextField.snp.bottom).offset(20)
    }
    nextButton.layer.cornerRadius = 4
    nextButton.clipsToBounds = true
    
    nextButtonHelper()
    
  }
  
}
